//
//  BaseTableViewController.swift
//  CandyDate
//
//  Created by zolobdz on 2017/10/26.
//  Copyright © 2017年 HeChao. All rights reserved.
//

import UIKit
import MJRefresh

class BaseTableViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    

    //MARK: Propertys
    
    lazy var emptyView = UIView()//EmptyView(frame: CGRect.zero)
    
    // 顶部刷新
    lazy var header = MJRefreshNormalHeader()
    // 底部刷新
    lazy var footer = MJRefreshAutoNormalFooter()
    var tableView = UITableView()
    
    
    //MARK: LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: 0.1)) // CGRect.zero的话在.grouped情况下会不起作用
        tableView.tableFooterView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: 0.1))
        tableView.sectionFooterHeight = 0.1
        tableView.sectionHeaderHeight = 0.1
        tableView.separatorColor = CDColor.cellSeparatorLineColor
        tableView.clipsToBounds = true
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        view.addSubview(emptyView)
        emptyView.isHidden = true
        tableView.snp.remakeConstraints { (maker) in
            maker.left.right.equalTo(view)
            if #available(iOS 11.0, *){
                // ios11以上
                maker.top.equalTo(view.safeAreaLayoutGuide)
                maker.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            }
            else
            {
                maker.top.equalTo(topLayoutGuide.snp.bottom)
                maker.bottom.equalTo(bottomLayoutGuide.snp.top)
            }
        }
        emptyView.snp.remakeConstraints { (maker) in
            maker.edges.equalTo(tableView)
        }
        
        header.setRefreshingTarget(self, refreshingAction: #selector(pullRefresh))
        header.lastUpdatedTimeLabel.isHidden = true
//        header.setTitle(LFConfigParams.pullIdeaText, for: .idle)
//        header.setTitle(LFConfigParams.pullIdeaText, for: .pulling)
//        header.setTitle(LFConfigParams.pullingText, for: .refreshing)
        tableView.mj_header = header
        
        footer.setRefreshingTarget(self, refreshingAction: #selector(loadMore))
//        footer.setTitle(LFConfigParams.loadMoreText, for: .idle)
//        footer.setTitle(LFConfigParams.loadMoreText, for: .pulling)
//        footer.setTitle(LFConfigParams.loadingMoreText, for: .refreshing)
        tableView.mj_footer = footer
        header.isHidden = true
        footer.isHidden = true
        
        emptyView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(emptyViewTapAction)))
        
    }
    
    //MARK: Override
    
    //MARK: Action
    
    //MARK: Delegate
    
//    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = UITableViewCell()
//        return cell
//    }
//
//    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return 0
//    }
    
    //MARK: Public
    
    func setupEmtyView(_ buttonTitle: String?, tipString: String, image: UIImage? = nil) {
//        emptyView.imageView.image = image
//        if buttonTitle != nil, !buttonTitle!.isEmpty
//        {
//            emptyView.actionButton.isHidden = false
//            emptyView.actionButton.setTitle(buttonTitle, for: .normal)
//            emptyView.imageView.image = UIImage(resource: R.image.them_icon_light)
//        }
//        emptyView.titleLabel.text = tipString
    }
    
    func showEmptyView() {
        emptyView.isHidden = false
    }
    func hidEmptyView() {
        emptyView.isHidden = true
    }
    
    @objc func emptyViewTapAction() {
        emptyView.isHidden = true
        if !header.isHidden
        {
            header.beginRefreshing()
        }
    }
        
    
    //MARK: Private
    
    // MARK: Pull Refresh & LoadMore
    @objc func pullRefresh() {
        
    }
    @objc func loadMore() {
        
    }

}
